home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / vctcimage.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-05-31  |  8.0 KB  |  322 lines

  1. // VCTCImage.cpp : Implementation of CVCTCImage
  2. #include "stdafx.h"
  3. #include "VCTCImageTool.h"
  4. #include "VCTCImage.h"
  5. #include "xdbtool_i.c"
  6. #include "gxties_i.c"
  7. #include "imsigx_i.c"
  8. #include "tcimage.h"
  9.  
  10.  
  11. COleVariant t(-1L, VT_BOOL);
  12. COleVariant f(0L, VT_BOOL);
  13. COleVariant missing((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CVCTCImage
  17. STDMETHODIMP CVCTCImage::get_ClassID(BSTR *pVal)
  18. {
  19.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  20.     HRESULT hRes = E_FAIL;
  21.     LPOLESTR olestr = NULL;
  22.  
  23.     try
  24.     {
  25.         if(SUCCEEDED( ::StringFromCLSID(CLSID_VCTCImage, &olestr)))
  26.         {
  27.             *pVal = ::SysAllocString(olestr);
  28.             hRes = S_OK;
  29.         }
  30.     }
  31.     catch (...)
  32.     {
  33.         TRACE_EXCEPTION("CVCTCImage::get_ClassID")
  34.     }
  35.  
  36.     if (olestr != NULL)
  37.     CoTaskMemFree(olestr);
  38.  
  39.     return hRes;
  40. }
  41.  
  42. STDMETHODIMP CVCTCImage::get_Description(BSTR *pVal)
  43. {
  44.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  45.  
  46.     HRESULT hRes = E_FAIL;
  47.     // Get the description from the resource
  48.     CString strResult;
  49.     if (strResult.LoadString(IDS_DESCRIPTION))
  50.     {
  51.         hRes = S_OK;
  52.         *pVal = strResult.AllocSysString();
  53.     }
  54.     return hRes;
  55. }
  56.  
  57. STDMETHODIMP CVCTCImage::GetToolInfo(VARIANT *CommandNames, VARIANT *MenuCaptions, VARIANT *StatusPrompts, VARIANT *ToolTips, VARIANT *Enabled, VARIANT *WantsUpdates, long *ret)
  58. {
  59.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  60.     HRESULT hRes = E_FAIL;
  61.     
  62.     SAFEARRAYBOUND bound = {NUM_TOOLS, 0};
  63.     SAFEARRAYBOUND bound1[2] = {{NUM_TOOLS, 0}, {2, 0}};
  64.  
  65.     CString cstrText;
  66.     CString cstrTextTemp;
  67.  
  68.     BSTR bstrCommandName = NULL;
  69.     BSTR bstrMenuCaption = NULL;
  70.     BSTR bstrStatusPrompt = NULL;
  71.     BSTR bstrToolTip = NULL;
  72.     BOOL bEnabled = TRUE;
  73.     BOOL bWantsUpdates = TRUE;
  74.  
  75.     
  76.     long lIndicies = 0;
  77.     long lIndicies1[2];
  78.     lIndicies1[0] = 0;
  79.     lIndicies1[1] = 0;
  80.     try
  81.     {
  82.         
  83.         hRes = SafeArrayRedim(CommandNames->parray, &bound);
  84.         CHECK_HRESULT(hRes)
  85.  
  86.         MenuCaptions->parray = SafeArrayCreate(VT_BSTR, 2, bound1);
  87.  
  88.         hRes = SafeArrayRedim(StatusPrompts->parray, &bound);
  89.         CHECK_HRESULT(hRes)
  90.  
  91.         hRes = SafeArrayRedim(ToolTips->parray, &bound);
  92.         CHECK_HRESULT(hRes)
  93.  
  94.         hRes = SafeArrayRedim(Enabled->parray, &bound);
  95.         CHECK_HRESULT(hRes)
  96.  
  97.         hRes = SafeArrayRedim(WantsUpdates->parray, &bound);
  98.         CHECK_HRESULT(hRes)
  99.  
  100.         cstrText.LoadString (IDS_MAINMENU);
  101.         cstrText.Insert(cstrText.GetLength (), '|');
  102.         cstrTextTemp.LoadString (IDS_SUBMENU);
  103.         cstrText.Insert(cstrText.GetLength (), cstrTextTemp);
  104.         cstrText.Insert(cstrText.GetLength (), '|');
  105.         cstrTextTemp.LoadString (IDS_MENUCAPTION);
  106.         cstrText.Insert(cstrText.GetLength (), cstrTextTemp);
  107.         cstrTextTemp.Empty ();
  108.  
  109.         cstrText += "#CMD_SDKINSERTFILE";
  110.  
  111.         bstrCommandName = cstrText.AllocSysString();
  112.         hRes = SafeArrayPutElement(CommandNames->parray, &lIndicies, (void *)bstrCommandName);
  113.         CHECK_HRESULT(hRes)
  114.  
  115.         cstrText.Empty ();
  116.         cstrText.LoadString (IDS_MENUCAPTION); 
  117.         bstrMenuCaption = cstrText.AllocSysString();
  118.         hRes = SafeArrayPutElement(MenuCaptions->parray, lIndicies1, (void *)bstrMenuCaption);
  119.         CHECK_HRESULT(hRes)
  120.         cstrText.Empty ();
  121.  
  122. //         cstrText.LoadString (IDS_PROMT0);
  123. //        bstrStatusPrompt = cstrText.AllocSysString();
  124. //        hRes = SafeArrayPutElement(StatusPrompts->parray, &lIndicies, (void *)bstrStatusPrompt);
  125. //        CHECK_HRESULT(hRes)
  126. //        cstrText.Empty ();
  127.  
  128.         cstrText.LoadString (IDS_TOOLTIPS);; 
  129.         bstrToolTip = cstrText.AllocSysString();
  130.         hRes = SafeArrayPutElement(ToolTips->parray, &lIndicies, (void *)bstrToolTip);
  131.         CHECK_HRESULT(hRes)
  132.  
  133.         hRes = SafeArrayPutElement(Enabled->parray, &lIndicies, (void *)&bEnabled);
  134.         CHECK_HRESULT(hRes)
  135.  
  136.         hRes = SafeArrayPutElement(WantsUpdates->parray, &lIndicies, (void *)&bWantsUpdates);
  137.         CHECK_HRESULT(hRes)
  138.  
  139. // put default toolbar name
  140.         lIndicies1[0] = 0;
  141.         lIndicies1[1] = 1;
  142.  
  143.         cstrText.LoadString (IDS_TOOLBAR_NAME); 
  144.         bstrMenuCaption = cstrText.AllocSysString();
  145.  
  146.         hRes = SafeArrayPutElement(MenuCaptions->parray, lIndicies1, (void *)bstrMenuCaption);
  147.         CHECK_HRESULT(hRes)
  148.         cstrText.Empty ();
  149.  
  150.         *ret = NUM_TOOLS;
  151.     }
  152.     catch (...)
  153.     {
  154.         if (SUCCEEDED(hRes))
  155.             hRes = E_FAIL;
  156.         TRACE_EXCEPTION("CVCTCImage::GetToolInfo")
  157.     }
  158.     return hRes;
  159. }
  160.  
  161. STDMETHODIMP CVCTCImage::CopyBitmap(VARIANT_BOOL LargeButtons, VARIANT_BOOL Monochrome, VARIANT_BOOL *ret)
  162. {
  163.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  164.  
  165.     // TODO: Add your implementation code here
  166.  
  167.     return S_OK;
  168. }
  169.  
  170.  
  171. STDMETHODIMP CVCTCImage::GetPicture(VARIANT_BOOL LargeButtons, VARIANT_BOOL Monochrome, IDispatch **ret)
  172. {
  173.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  174.     LPTSTR IDB = 0;
  175.  
  176.     HRESULT hRes = E_FAIL;
  177.     try
  178.     {
  179.         if(LargeButtons)
  180.         {
  181.             IDB = MAKEINTRESOURCE(IDB_BMP2);
  182.         }
  183.         else
  184.         {
  185.             IDB = MAKEINTRESOURCE(IDB_BMP1);
  186.         }
  187.         HBITMAP pBmp = ::LoadBitmap(AfxGetResourceHandle(), IDB);
  188.  
  189.         PICTDESC pict;
  190.         pict.cbSizeofstruct = sizeof(pict);
  191.         pict.picType = PICTYPE_BITMAP;
  192.         pict.bmp.hbitmap = pBmp;
  193.         pict.bmp.hpal = NULL;
  194.  
  195.         hRes = OleCreatePictureIndirect(&pict, IID_IDispatch, TRUE, (void**)ret);
  196.  
  197.         CHECK_HRESULT(hRes)
  198.         hRes = S_OK;
  199.     }
  200.     catch (...)
  201.     {
  202.         TRACE_EXCEPTION("CVCTCImage::GetPicture")
  203.     }
  204.  
  205.     return S_OK;
  206. }
  207. void InsertImage(CString sImageFilePath, IDrawing *pDrawing)
  208. {
  209.     ITCImageManager pImageManager;
  210.     COleException oerr;
  211.     CString sname("Test name");
  212.     IMatrix* pMatrix = NULL;
  213.     double dx = 100;
  214.     double dy = 100;
  215.     BOOL bReference = TRUE;
  216.     pImageManager.CreateDispatch("TCImage.TCImageManager",&oerr);
  217.     long lImageStyle = pImageManager.AddImageStyle(pDrawing, sname, sImageFilePath, bReference);
  218.  
  219.     HRESULT hRes = pDrawing->get_UCS(&pMatrix);
  220.     IGraphic* pGraphic = (IGraphic*)pImageManager.CreateImageObject(pDrawing, lImageStyle, pMatrix, dx, dy);
  221.     Properties* pProps = NULL;;
  222.     Property* pProp = NULL;
  223.     hRes = pGraphic->get_Properties(&pProps);
  224.     COleVariant varIndex("%#$AUX@_ShowImage");
  225.     COleVariant varValue(1L);
  226.     hRes = pProps->get_Item (&varIndex, &pProp);
  227.     hRes = pProp->put_Value (1, &varValue);
  228.     RELEASE(pMatrix);
  229.     RELEASE(pProp);
  230.     RELEASE(pProps);
  231.     RELEASE(pGraphic);
  232. }
  233.  
  234. STDMETHODIMP CVCTCImage::Run(IDispatch *ThisTool, VARIANT_BOOL *ret)
  235. {
  236.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  237.     HRESULT hRes = E_FAIL;
  238.     CString sExt = "Pictures (*.bmp;*.gif;*.jpg;*png;*.tif;*.pcx;*.ico;*.tga;*.wmf)|*.bmp;*.gif;*.jpg;*png;*.tif;*.pcx;*.ico;*.tga;*.wmf||";
  239.  
  240.     
  241.     CFileDialog fDlg(TRUE, NULL, NULL,OFN_ENABLESIZING ,sExt, NULL);
  242.     int res = fDlg.DoModal ();
  243.     if (res != IDOK)
  244.     {
  245.         return S_OK;
  246.     }
  247.  
  248.     CString strOutput = fDlg.GetPathName();
  249.     CComPtr<IApplication> pApp;
  250.     CComPtr<IDrawing> pActDr;
  251.     CComPtr<View> pView;
  252.     CComPtr<Tool> pTool;
  253.     try
  254.     {
  255.         hRes = ThisTool->QueryInterface(IID_Tool, (void **)&pTool);
  256.         CHECK_HRESULT(hRes)
  257.         hRes = pTool->get_Application(&pApp);
  258.         CHECK_HRESULT(hRes)
  259.         hRes = pApp->get_ActiveDrawing(&pActDr);
  260.         if (SUCCEEDED(hRes) && pActDr)
  261.         {
  262.             InsertImage(strOutput, pActDr);
  263.             hRes = pActDr->get_ActiveView(&pView);
  264.             CHECK_HRESULT(hRes);
  265.             hRes = pView->ZoomToExtents();
  266.             CHECK_HRESULT(hRes);
  267.         }
  268.         
  269.     }
  270.     catch(...)
  271.     {
  272.         TRACE_EXCEPTION("CVCTCImage::Run")
  273.     }
  274.     return S_OK;
  275. }
  276.  
  277. STDMETHODIMP CVCTCImage::UpdateToolStatus(IDispatch *ThisTool, VARIANT_BOOL *Enabled, VARIANT_BOOL *Checked, VARIANT_BOOL *ret)
  278. {
  279.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  280.     HRESULT hRes = E_FAIL;
  281.  
  282.     try 
  283.     {
  284.         // do not allow to run this tool if it is already runned
  285.         *ret = TRUE;
  286.         hRes = S_OK;
  287.  
  288.     }
  289.     catch (...)
  290.     {
  291.         TRACE_EXCEPTION("CVCTCImage::UpdateToolStatus")
  292.     }
  293.     return hRes;
  294. }
  295. STDMETHODIMP CVCTCImage::Initialize(IDispatch *ThisTool, VARIANT_BOOL *ret)
  296. {
  297.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  298.     /*
  299.     m_pIApplication = NULL;
  300.     m_pITool = NULL;
  301.     m_pTool = NULL;
  302.     m_dwEventConnection = NULL;
  303.     m_pDragGraphic = NULL;
  304.     m_bDragging  = FALSE;
  305.     m_pTCADView = NULL;
  306.     m_bInit = FALSE;
  307.     m_pIToolEvents = NULL;
  308.     */
  309.     return S_OK;
  310. }
  311.  
  312. STDMETHODIMP CVCTCImage::Terminate(IDispatch *ThisTool)
  313. {
  314.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  315.  
  316.     // TODO: Add your implementation code here
  317.     return S_OK;
  318. }
  319.  
  320.  
  321.  
  322.